package com.fansl.koala.quickdev.module.users.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fansl.koala.quickdev.common.bean.PageBean;
import com.fansl.koala.quickdev.common.bean.R;
import com.fansl.koala.quickdev.common.constants.CommonConstants;
import com.fansl.koala.quickdev.module.users.entity.SysUserRole;
import com.fansl.koala.quickdev.module.users.service.ISysUserRoleService;
import com.fansl.koala.quickdev.module.users.vo.SysUserRoleVo;
import com.fansl.koala.quickdev.module.users.bean.dto.SysUserRoleDTO;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色表 前端控制器
 * </p>
 *
 * @author fansl
 * @since 2020-04-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("/users/sys-user-role")
public class SysUserRoleController {
    private final ISysUserRoleService sysUserRoleService;

    /**
    * 添加
    * @param sysUserRoleDTO 参数
    * @return true/false
    */
    @PostMapping("")
    public Mono<R<Boolean>> add(@RequestBody SysUserRoleDTO sysUserRoleDTO) {
        SysUserRole sysUserRole = sysUserRoleDTO2Bean(sysUserRoleDTO);
        boolean b = sysUserRoleService.save(sysUserRole);
        return Mono.just(R.ok(b));
    }

    /**
    * 根据主键更新
    * @param sysUserRoleDTO 参数
    * @return true/false
    */
    @PutMapping("")
    public Mono<R<Boolean>> updateById(@RequestBody SysUserRoleDTO sysUserRoleDTO) {
        SysUserRole sysUserRole = sysUserRoleDTO2Bean(sysUserRoleDTO);
        boolean b = sysUserRoleService.updateById(sysUserRole);
        return Mono.just(R.ok(b));
    }

    /**
    * 根据主键获取信息
    * @param id 主键
    * @return 查询数据
    */
    @GetMapping("/{id}")
    public Mono<R<SysUserRoleVo>> getById(@PathVariable Long id) {
        SysUserRole sysUserRole = sysUserRoleService.getById(id);
        SysUserRoleVo sysUserRoleVo = sysUserRole2Vo(sysUserRole);
        return Mono.just(R.ok(sysUserRoleVo));
    }

    /**
    * 根据主键删除
    * @param id 主键
    * @return true/false
    */
    @DeleteMapping("/{id}")
    public Mono<R<Boolean>> delById(@PathVariable Long id) {
       boolean b = sysUserRoleService.removeById(id);
       return Mono.just(R.ok(b));
    }

    /**
    * 分页查询
    * @param page 分页询参数
    * @param sysUserRole 查询参数
    * @return 查询数据
    */
    @GetMapping("/page")
    public Mono<R<PageBean<SysUserRoleVo>>> page(PageBean<SysUserRoleVo> page, SysUserRole sysUserRole) {
        //分页参数
        Page<SysUserRole> queryPage = new Page<>();
        queryPage.setCurrent(page.getPageNo());
        queryPage.setSize(page.getPageSize());
        if (StrUtil.isNotBlank(page.getSortOrder()) && StrUtil.isNotBlank(page.getSortField())) {
            OrderItem orderItem = new OrderItem();
            orderItem.setAsc(page.getSortOrder().equals(CommonConstants.PAGE_QUERY_ORDER_ASC));
            orderItem.setColumn(StrUtil.toUnderlineCase(page.getSortField()));
            queryPage.setOrders(Collections.singletonList(orderItem));
        }
        //查询
        LambdaQueryWrapper<SysUserRole> queryWrapper = Wrappers.lambdaQuery(sysUserRole);
        Page<SysUserRole> result = sysUserRoleService.page(queryPage, queryWrapper);
        //结果转vo
        List<SysUserRoleVo> data = result.getRecords().stream()
            .map(this::sysUserRole2Vo)
            .collect(Collectors.toList());
        //转换结果
        page.setData(data);
        page.setTotalCount(result.getTotal());
        page.setTotalPage(result.getPages());
        return Mono.just(R.ok(page));
    }

    /**
    * Vo对象转换
    * @param sysUserRole 源对象
    * @return 目标对象
    */
    private SysUserRoleVo sysUserRole2Vo(SysUserRole sysUserRole){
        SysUserRoleVo target = new SysUserRoleVo();
        BeanUtil.copyProperties(sysUserRole,target);
        return target;
    }

    /**
    * DTO对象转换
    * @param sysUserRoleDTO 源对象
    * @return 目标对象
    */
    private SysUserRole sysUserRoleDTO2Bean(SysUserRoleDTO sysUserRoleDTO){
        SysUserRole target = new SysUserRole();
        BeanUtil.copyProperties(sysUserRoleDTO,target);
        return target;
    }
}
